using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections.ObjectModel;
using Microsoft.Health.PatientConnect;

namespace DCApp
{
    public partial class CheckStatusDlg : Form
    {
        public CheckStatusDlg()
        {
            InitializeComponent();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            btnUpdate.Text = "Processing...";
            btnUpdate.Enabled = false;

            int pastDays = 0;
            if (radioPast7.Checked)
            {
                pastDays = 7;
            }
            else if (radioPast30.Checked)
            {
                pastDays = 30;
            }


            try
            {
                Collection<ValidatedPatientConnection> connectionList = DCHelper.Functions.GetValidatedConnectionsInPastDays(pastDays);
                foreach (ValidatedPatientConnection validatedConnection in connectionList)
                {

                    //Update the XML store with the PersonId and RecordId of the user who has authorized our application
                    //Also set the HasAuthorized flag to true for this participant
                    XmlStore.UpdateParticipantToAuthorizedStatus(validatedConnection.ApplicationPatientId,
                        validatedConnection.PersonId.ToString(),
                        validatedConnection.RecordId.ToString());
                }
                
                string confirmTemplate = "Found {0} authorizations and participant status updated";
                string confirmString = String.Format(confirmTemplate, connectionList.Count.ToString());
                MessageBox.Show(confirmString);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Exception Occured:" + ex.Message);
            }
            finally
            {
                btnUpdate.Text = "&Update participant status";
                btnUpdate.Enabled = true;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}